/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flask.colorpicker.cameralibrary.listener;

import com.flask.colorpicker.cameralibrary.JCameraView;
import com.flask.colorpicker.cameralibrary.state.CameraMachine;
import com.flask.colorpicker.cameralibrary.util.LogUtil;

import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.media.image.Image;
import ohos.media.image.ImageReceiver;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.ImageFormat;

/**
 * IImageArrivalListener Impl
 */
public class IImageArrivalListenerImpl implements ImageReceiver.IImageArrivalListener {
    private final CameraMachine mMachine;

    public IImageArrivalListenerImpl(CameraMachine machine) {
        this.mMachine = machine;
    }

    @Override
    public void onImageArrival(ImageReceiver receiver) {
        if (receiver == null) {
            takePictureError("ImageReceiver is null");
            return;
        }
        Image readNextImage = receiver.readNextImage();
        LogUtil.error(
                LogUtil.DEFAULT_TAG, "readLatestImage:" + readNextImage + ":LastImage:" + receiver.readLatestImage());
        if (readNextImage == null) {
            receiver.release();
            takePictureError("ImageReceiver no readNextImage");
            return;
        }
        Image.Component component = readNextImage.getComponent(ImageFormat.ComponentType.JPEG);
        if (component == null) {
            takePictureError("ImageReceiver component null");
            return;
        }
        byte[] bytes = new byte[component.remaining()];
        component.read(bytes);

        ImageSource source = ImageSource.create(component.getBuffer(), null);
        if (source == null) {
            takePictureError("ImageReceiver ImageSource null");
            return;
        }
        PixelMap pixelMap = source.createPixelmap(null);
        if (pixelMap == null) {
            takePictureError("ImageReceiver pixelMap null");
            return;
        }
        new EventHandler(EventRunner.getMainEventRunner())
                .postTask(
                        () -> {
                            mMachine.getView().showPicture(pixelMap, true);
                            mMachine.setState(mMachine.getBorrowPictureState());
                            mMachine.restart();
                        });
    }

    private void takePictureError(String error) {
        LogUtil.error(LogUtil.DEFAULT_TAG, "takePictureError:" + error);
        new EventHandler(EventRunner.getMainEventRunner())
                .postTask(
                        () -> {
                            mMachine.getView().resetState(JCameraView.TYPE_PICTURE);
                            mMachine.setState(mMachine.getPreviewState());
                        });
    }
}
